package com.zw.demo01.service.impl;
import com.zw.demo01.common.datasource.DruidDataSourceUtil;
import com.zw.demo01.constants.Constants;
import com.zw.demo01.mapper.DynamicSqlMapper;
import com.zw.demo01.service.DynamicDataSourceService;
import com.zw.demo01.utils.DataUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: DynamicDataSourceServiceImpl
 * @Description TODO
 * @Author: zzw
 * @Date: 2023/3/28 14:45
 * @Version 1.0.0
 */
@Slf4j
@Service("dynamicDataSourceService")
public class DynamicDataSourceServiceImpl implements DynamicDataSourceService {

    @Autowired
    private DynamicSqlMapper dynamicSqlMapper;

    /**
     * @MonthName： dynamicExecutive
     * @Description： 加载动态数据源执行SQL
     * @Param： {
     * "dbip":"IP地址",
     * "dbport":"端口号",
     * "dbname":"数据库名称",
     * "dbuser":"用户名称",
     * "dbpwd":"密码",
     * "type":"执行类型：SELECT、INSERT、UPDATE、DELETE",
     * "paramSQL":"需要执行的SQL",
     * "param":{} // SQL中的参数
     * }
     * @return： java.util.Map<java.lang.String, java.lang.Object>
     **/
    @Override
    public Map<String, Object> dynamicExecutive(Map<String, Object> params) {
        Map<String, Object> result = null;
        try {
            DruidDataSourceUtil.addOrChangeDataSource(
                    String.valueOf(params.get("dbip")),
                    String.valueOf(params.get("dbport")),
                    String.valueOf(params.get("dbname")),
                    String.valueOf(params.get("dbuser")),
                    String.valueOf(params.get("dbpwd"))
            );
        } catch (Exception e) {
            log.error("=====创建据库连接异常:{}", e);
            result.put("data", "创建据库连接异常，请检查连接信息是否有误！");
        }

        try {
            // 执行动态SQL
            Object data = null;
            String type = String.valueOf(params.get("type"));
            String paramSQL = String.valueOf(params.get("paramSQL"));
            Map<String, Object> param = (HashMap) params.get("param");

            // 参数替换
            String sql = DataUtils.strReplace(paramSQL, param);

            log.info("======请求SQL语句:{}======", sql);

            switch (type) {
                case Constants.SELECT:
                    data = dynamicSqlMapper.dynamicsSelect(sql);
                    break;
                case Constants.INSERT:
                    data = dynamicSqlMapper.dynamicsInsert(sql);
                    break;
                case Constants.UPDATE:
                    data = dynamicSqlMapper.dynamicsUpdate(sql);
                    break;
                case Constants.DELETE:
                    data = dynamicSqlMapper.dynamicsDelete(sql);
                    break;
                default:
                    data = "请求参数【type】有误，请核查！";
                    break;
            }

            result = new HashMap<>();
            result.put("data", data);
        } catch (Exception e) {
            log.error("=====执行SQL异常:{}", e);
            result.put("data", "执行SQL异常，请检查SQL语句是否有误！");
        }
        return result;
    }

}
